<?php
/***
	PDMS元件材料的管理，包括：
	1. 分类查询；
	2. 新增；
	3. 删除；
	4. 修改。
***/
class Pdms_materialcode extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('pdms_materialcode_model');
		$this->load->model('pdms_type_model');
		$this->load->helper('url');
	}
	
	/***
	新增、修改、过滤一体化设计；
	删除采用异步。
	
	***/
	public function index($type_code = NULL, $id = 0, $flag_del = 0) {
			
		//如果POST form1表示传来类别信息，可以列表了
		if (isset($_POST['form1'])) {
			$type_code = $_POST['type_code'];
			$data['type_code'] = $type_code;
			$data['materialcodes'] = $this->pdms_materialcode_model->get_by_type($type_code);
			$type = $this->pdms_type_model->get_by_code($type_code);
			$data['type_des'] = $type['des'];
		}
		
		//如果POST form2表示新建新建表单传来数据
		if (isset($_POST['form2'])) {
			$type_code = $_POST['type_code'];
			$data['type_code'] = $type_code;
			$this->pdms_materialcode_model->create();
			$data['materialcodes'] = $this->pdms_materialcode_model->get_by_type($type_code);
			$type = $this->pdms_type_model->get_by_code($type_code);
			$data['type_des'] = $type['des'];
		}
		
		//如果POST form3表示修改表单传来数据
		if (isset($_POST['form3'])) {
			$type_code = $_POST['type_code'];
			$data['type_code'] = $type_code;
			$data['materialcodes'] = $this->pdms_materialcode_model->get_by_type($type_code);
			$id = $_POST['id'];
			$this->pdms_materialcode_model->edit($id);
			$type = $this->pdms_type_model->get_by_code($type_code);
			$data['type_des'] = $type['des'];
		}
		//如果$flag_del<>0，要去删除一条记录
		if ($flag_del <> 0) {
			$materialcodes = array();
			$materialcodes = $this->pdms_materialcode_model->get_by_id($id);
			$data['type_code'] = $materialcodes['typecode'];
			$type_code = $data['type_code'];
			$type = $this->pdms_type_model->get_by_code($type_code);
			$data['type_des'] = $type['des'];
			$this->pdms_materialcode_model->del($id);
			$data['materialcodes'] = $this->pdms_materialcode_model->get_by_type($type_code);
		}
		
		$data['types'] = $this->pdms_type_model->get_all();
		$data['title'] = "材料表";
		
		$this->load->library('parser');
		
		$this->load->view('templates/pdms_head');
		$this->load->view('templates/pdms_menu');
		$this->parser->parse('pdms_materialcode/form_type',$data);
		
		if ($type_code <> NULL) {
		if ($id <> 0) {
			if ($flag_del <> 0) {
				$this->parser->parse('pdms_materialcode/form_create', $data);
			} else {
				$materialcodes = array();
				$materialcodes = $this->pdms_materialcode_model->get_by_id($id);
				$data['id'] = $id;
				$data['code'] = $materialcodes['code'];
				$data['des'] = $materialcodes['des'];
				$data['type_code'] = $materialcodes['typecode'];
				$type_code = $data['type_code'];
				$data['materialcodes'] = $this->pdms_materialcode_model->get_by_type($type_code);
				$type = $this->pdms_type_model->get_by_code($type_code);
				$data['type_des'] = $type['des'];
				$this->parser->parse('pdms_materialcode/form_edit', $data);
			}
			
		} else {
			$this->parser->parse('pdms_materialcode/form_create', $data);
		}
		

		$this->parser->parse('pdms_materialcode/list', $data);
		}
		$this->load->view('templates/foot');
		
	}
}